{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Synthesis\n",
    "\n",
    "Using simples tecniques for the synthesis of leads on the ECG\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Essential Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading the ECG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "samplePath = \"samples/1005639.txt\"\n",
    "\n",
    "EcgHeaders = [\n",
    "    \"sample\",\n",
    "    \"LI\", \n",
    "    \"LII\", \n",
    "    \"LIII\", \n",
    "    \"aVR\", \n",
    "    \"aVF\", \n",
    "    \"aVL\",\n",
    "    \"V1\",\n",
    "    \"V2\",\n",
    "    \"V3\",\n",
    "    \"V4\",\n",
    "    \"V5\",\n",
    "    \"V6\"\n",
    "]\n",
    "\n",
    "ecgDf = pd.read_csv(samplePath)\n",
    "ecgDf.columns = EcgHeaders\n",
    "ecgDf = ecgDf.set_index(\"sample\")\n",
    "\n",
    "ecgDf.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparative Ecg Plot Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def comparativeEcgPlot(ecg1, label1, ecg2, label2, title):\n",
    "\tplt.figure(figsize = (12, 6))\n",
    "\n",
    "\tplt.plot(ecg1, label = label1)\n",
    "\tplt.plot(ecg2, label = label2)\n",
    "\n",
    "\tplt.title(title)\n",
    "\n",
    "\tplt.xlabel(\"Amostra\")\n",
    "\tplt.ylabel(\"Dpp\")\n",
    "\n",
    "\tplt.legend()\n",
    "\n",
    "\tplt.show()\n",
    "\tplt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Synthesis of the derivation aVR from LI and LII"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$ aVR_{rec} = - \\frac{LI + LII}{\\sqrt{3}} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVRRec = -(ecgDf[\"LI\"] + ecgDf[\"LII\"]) / np.sqrt(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVRRecCorr = ecgDf[\"aVR\"].corr(\n",
    "    other = aVRRec, \n",
    "    method = \"pearson\"\n",
    ")\n",
    "aVRRecCorr = np.round(aVRRecCorr, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $aVR_{rec}$ vs aVR plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparativeEcgPlot(\n",
    "    ecg1 = aVRRec,\n",
    "    label1 = \"$aVR_{rec}$\",\n",
    "    \n",
    "    ecg2 = ecgDf[\"aVR\"],\n",
    "    label2 = \"aVR\",\n",
    "    \n",
    "    title = \"$aVR_{rec}$ vs aVR. Correlation = \" + str(aVRRecCorr)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Synthesis of the derivation aVL from LI and LII"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$ aVL_{rec} = \\frac{2 * LI - LII}{\\sqrt{3}} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVLRec = (2 * ecgDf[\"LI\"] - ecgDf[\"LII\"]) / np.sqrt(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVLRecCorr = ecgDf[\"aVL\"].corr(\n",
    "    other = aVLRec, \n",
    "    method = \"pearson\"\n",
    ")\n",
    "aVLRecCorr = np.round(aVLRecCorr, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $aVL_{rec}$ vs aVL plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparativeEcgPlot(\n",
    "    ecg1 = aVLRec,\n",
    "    label1 = \"$aVL_{rec}$\",\n",
    "    \n",
    "    ecg2 = ecgDf[\"aVL\"],\n",
    "    label2 = \"aVL\",\n",
    "    \n",
    "    title = \"$aVL_{rec}$ vs aVL. Correlation = \" + str(aVLRecCorr)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Synthesis of the derivation aVF from LI and LIII"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$ aVF_{rec} = \\frac{LII + LIII}{\\sqrt{3}} $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVFRec = (ecgDf[\"LII\"] + ecgDf[\"LIII\"]) / np.sqrt(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "aVFRecCorr = ecgDf[\"aVF\"].corr(\n",
    "    other = aVFRec, \n",
    "    method = \"pearson\"\n",
    ")\n",
    "aVFRecCorr = np.round(aVFRecCorr, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $aVF_{rec}$ vs aVF plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparativeEcgPlot(\n",
    "    ecg1 = aVFRec,\n",
    "    label1 = \"$aVF_{rec}$\",\n",
    "    \n",
    "    ecg2 = ecgDf[\"aVF\"],\n",
    "    label2 = \"aVF\",\n",
    "    \n",
    "    title = \"$aVL_{rec}$ vs aVL. Correlation = \" + str(aVFRecCorr)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Synthesis of the derivation LIII from LI and LII"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$ LIII_{rec} = LII - LI $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "VIIIRec = ecgDf[\"LII\"] - ecgDf[\"LI\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "VIIIRecCorr = ecgDf[\"LIII\"].corr(\n",
    "    other = VIIIRec, \n",
    "    method = \"pearson\"\n",
    ")\n",
    "VIIIRecCorr = np.round(VIIIRecCorr, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $aVF_{rec}$ vs aVF plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "comparativeEcgPlot(\n",
    "    ecg1 = VIIIRec,\n",
    "    label1 = \"$LIII_{rec}$\",\n",
    "    \n",
    "    ecg2 = ecgDf[\"LIII\"],\n",
    "    label2 = \"LIII\",\n",
    "    \n",
    "    title = \"$LIII_{rec}$ vs LIII. Correlation = \" + str(VIIIRecCorr)\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
